﻿using UnityEngine;
using System.Collections;

public enum GameState {
	Running,
	Over
}

public class GameManager : MonoBehaviour {

	public static GameManager Instance;

	public FallerControl fallerControl = null;
	public Vector2 fallerStartPos = new Vector2(1.3f, 0.27f);
	public Vector2 firstPlatformPos = new Vector2(1.36f, -0.94f);
	public float rateOfAddLevel = 5.0f;		//多久增加一次等級

	private GameState _gameState = GameState.Over;	//目前的遊戲狀態
	private int _nowLevel = 0;						//目前的等級
	private int _gameScore = 0;						//目前的分數
	private float _nextTimeToAddLevel = 0.0f;		//下一次增加等級的時間	
	private bool _addFirstScore = false;			//第一次加分 (過濾掉第一個平台的分數)

	public GameState gameState { get { return this._gameState; } }
	public int nowLevel { get { return this._nowLevel; } }

	void Awake() {
		Instance = this;
		if (Screen.fullScreen) Screen.fullScreen = false;
		if (this.fallerControl) {
			this.fallerControl.transform.localPosition = fallerStartPos;
			this.fallerControl.gameObject.SetActive(false);
		}
		this._gameState = GameState.Over;

	}

	void Start() {
		if (PlatformManager.Instance) {
			PlatformManager.Instance.enabled = false;
			PlatformManager.Instance.ClearAllPlatform();
		}
	}

	void Update() {
		if (this._gameState == GameState.Over) {
			if (Input.GetKeyDown(KeyCode.Space))
				GameStart();
		} else {
			if (Time.timeSinceLevelLoad > this._nextTimeToAddLevel) {
				//增加Level
				this._nextTimeToAddLevel = Time.timeSinceLevelLoad + this.rateOfAddLevel;
				this._nowLevel++;
				PlatformManager.Instance.UpdateAllPlatformSpeed(this._nowLevel);
			}
		}
	}

	void OnGUI() {
		int line = 0;
		GUI.Label(new Rect(Screen.width - 300, 0, 100, 30), "Score: " + this._gameScore);
		if (this._gameState == GameState.Over) {
			GUI.Label(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.5f + line++ * 30, 80, 30), "Game Over!");
			GUI.Label(new Rect(Screen.width * 0.5f - 100, Screen.height * 0.5f + line++ * 30, 320, 30), "Press [Space] key to Start Game!");
		} else {
			GUI.Label(new Rect(Screen.width - 200, 0, 100, 30), "Level: " + this._nowLevel);
		}
	}

	private void GameStart() {
		this._gameState = GameState.Running;
		this._gameScore = 0;
		this._nowLevel = 0;
		this._nextTimeToAddLevel = Time.timeSinceLevelLoad + this.rateOfAddLevel;
		this.fallerControl.transform.localPosition = fallerStartPos;
		this._addFirstScore = false;
		PlatformManager.Instance.enabled = true;
		//Creat First Platform
		Platform platform = PlatformManager.Instance.NextPlatform;
		platform.position = this.firstPlatformPos;
		PlatformManager.Instance.UpdateAllPlatformSpeed(this._nowLevel);
		this.fallerControl.gameObject.SetActive(true);
	}

	public void AddScore() {
		//計算加分
		if (this._addFirstScore)
			this._gameScore += (this._nowLevel + 1) * 10;
		else this._addFirstScore = true;
	}

	public void GameOver() {
		//遊戲結束
		this.fallerControl.gameObject.SetActive(false);
		PlatformManager.Instance.enabled = false;
		PlatformManager.Instance.ClearAllPlatform();
		this._gameState = GameState.Over;
	}
}
